package com.maolong.controller.manger;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maolong.entity.domain.User;
import com.maolong.entity.enumm.IdentityEnum;
import com.maolong.entity.format.Code;
import com.maolong.entity.format.RespResult;
import com.maolong.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/worker")
public class WorkController {
    @Autowired
    private LoginService loginService;
    /**
     * 获取所有普通用户信息
     * @param name
     * @return
     */
    @GetMapping
    public RespResult workGet(@RequestParam(required = false,value = "name") String name){
        log.info("name:{}",name);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("identify",3);
        List<User> list;
        if (name==null|| name.isEmpty()){
            list=loginService.list(queryWrapper);
        }else{
            queryWrapper.like("username",name);
            list=loginService.list(queryWrapper);
        }
        System.out.println(list);
        return new RespResult(list!=null?Code.USER_OK:Code.USER_FAIL,list,null);
    }

    /**
     * 添加普通用户
     * @param user
     * @return
     */
    @PostMapping
    public RespResult workPost(@RequestBody User user){
        user.setIdentify(IdentityEnum.USER);
        boolean save = loginService.save(user);
        return new RespResult(save==true?Code.USER_OK:Code.USER_FAIL,save,null);
    }

    /**
     * 删除普通用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public RespResult workDelete(@PathVariable(value = "id") Long id){
        log.info("id:{}",id);
        boolean removeById = loginService.removeById(id);
        return new RespResult(removeById==true?Code.USER_OK:Code.USER_FAIL,removeById,null);
    }

    /**
     * 修改普通用户信息
     * @param user
     * @return
     */
    @PutMapping
    public RespResult workPut(@RequestBody User user){
        boolean updateById = loginService.updateById(user);
        return new RespResult(updateById==true? Code.USER_OK:Code.USER_FAIL,updateById,null);
    }

}
